﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbProductsDal
	{
		public DbProductsDal()
		{
		}
 		#region Insert
 		public static int Insert(DbProducts dbProducts)
		{	
			string sql = @"Insert Into DbProducts (
					PName,
					PCode,
					PClassId,
					PAllClassIds,
					PKeywords,
					PDescription,
					PNote,
					PMarketPrice,
					PUserPrice,
					PDefaultImg,
					PBigDefaultImg,
					PSmallDefaultImg,
					POrder,
					PStatus,
					PIsHot,
					PHits,
					PIsNews,
					PAddTime,
					PShortInstr,
					PStoreAmount,
					PWeight,
					PDesigner,
					PSizeFit,
					PShippingReturns
				) values('{0}','{1}',{2},'{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}',{12},{13},{14},{15},{16},'{17}','{18}',{19},'{20}','{21}','{22}','{23}')";
			sql = string.Format(sql,
					SqlHelper.String(dbProducts.PName),
					SqlHelper.String(dbProducts.PCode),
					dbProducts.PClassId,
					SqlHelper.String(dbProducts.PAllClassIds),
					SqlHelper.String(dbProducts.PKeywords),
					SqlHelper.String(dbProducts.PDescription),
					dbProducts.PNote,
					SqlHelper.String(dbProducts.PMarketPrice),
					SqlHelper.String(dbProducts.PUserPrice),
					SqlHelper.String(dbProducts.PDefaultImg),
					SqlHelper.String(dbProducts.PBigDefaultImg),
					SqlHelper.String(dbProducts.PSmallDefaultImg),
					dbProducts.POrder,
					dbProducts.PStatus,
					dbProducts.PIsHot,
					dbProducts.PHits,
					dbProducts.PIsNews,
					dbProducts.PAddTime,
					SqlHelper.String(dbProducts.PShortInstr),
					dbProducts.PStoreAmount,
					SqlHelper.String(dbProducts.PWeight),
					dbProducts.PDesigner,
					dbProducts.PSizeFit,
					dbProducts.PShippingReturns
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbProducts dbProducts)
		{	
			string sql = @"Update DbProducts set 

					PName= '{0}',
					PCode= '{1}',
					PClassId= {2},
					PAllClassIds= '{3}',
					PKeywords= '{4}',
					PDescription= '{5}',
					PNote= '{6}',
					PMarketPrice= '{7}',
					PUserPrice= '{8}',
					PDefaultImg= '{9}',
					PBigDefaultImg= '{10}',
					PSmallDefaultImg= '{11}',
					POrder= {12},
					PStatus= {13},
					PIsHot= {14},
					PHits= {15},
					PIsNews= {16},
					PAddTime= '{17}',
					PShortInstr= '{18}',
					PStoreAmount= {19},
					PWeight= '{20}',
					PDesigner= '{21}',
					PSizeFit= '{22}',
					PShippingReturns= '{23}'
				where

					PId= {24}";
			sql = string.Format(sql,
					SqlHelper.String(dbProducts.PName),
					SqlHelper.String(dbProducts.PCode),
					dbProducts.PClassId,
					SqlHelper.String(dbProducts.PAllClassIds),
					SqlHelper.String(dbProducts.PKeywords),
					SqlHelper.String(dbProducts.PDescription),
					dbProducts.PNote,
					SqlHelper.String(dbProducts.PMarketPrice),
					SqlHelper.String(dbProducts.PUserPrice),
					SqlHelper.String(dbProducts.PDefaultImg),
					SqlHelper.String(dbProducts.PBigDefaultImg),
					SqlHelper.String(dbProducts.PSmallDefaultImg),
					dbProducts.POrder,
					dbProducts.PStatus,
					dbProducts.PIsHot,
					dbProducts.PHits,
					dbProducts.PIsNews,
					dbProducts.PAddTime,
					SqlHelper.String(dbProducts.PShortInstr),
					dbProducts.PStoreAmount,
					SqlHelper.String(dbProducts.PWeight),
					dbProducts.PDesigner,
					dbProducts.PSizeFit,
					dbProducts.PShippingReturns,
					dbProducts.PId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 pId)
		{
		
			string sql = @"Delete from DbProducts where 
					PId= {0}";
			sql = string.Format(sql,
					pId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbProducts a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbProducts Select(Int32 pId)
		{
			DbProducts dbProducts = null;
			string sql = @"select a.* from DbProducts a  where
					a.PId= {0}
";
			sql = string.Format(sql,
					pId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbProducts = new DbProducts(ds.Tables[0].Rows[0]);					
			return dbProducts;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbProducts a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbProducts a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbProducts a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
